package yl.hs.bmipfull.service.production.impl;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import yl.hs.bmipfull.daos.material.IWarehouseDao;
import yl.hs.bmipfull.pojo.material.WarehouseEntity;
import yl.hs.bmipfull.service.production.IPDWarehouseService;
import yl.hs.bmipfull.utils.DataState;
import yl.hs.bmipfull.utils.HsApp;
import yl.hs.bmipfull.utils.SearchFilterEngine;
import yl.hs.bmipfull.utils.Tl;
import yl.hs.bmipfull.utils.datastate.ActionType;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.PageResult;

import java.util.List;
import java.util.Map;

/**
 * 作用:  仓库信息服务层接口实现<br/>
 * 创建时间: 2021年05月19日 11时02分59秒 <br/>
 *
 * @author 张凯
 */
@Service
public class PDWarehouseServiceImpl extends ProductionBaseService implements IPDWarehouseService {

    @Autowired
    IWarehouseDao dao;

    @Override
    public List<WarehouseEntity> queryList(PageQuery pageQuery) throws Exception {
        pageQuery.checkSortSides(WarehouseEntity.class);
        if (pageQuery.getQuery().get("isRawMaterials") == null) {
            pageQuery.getQuery().put("isRawMaterials", 0);
        }
        var filters = SearchFilterEngine.generateQueryList(pageQuery.getFilters(),WarehouseEntity.class);

        return dao.queryList(pageQuery.getQuery(),filters);
    }

    @Override
    public List<WarehouseEntity> queryList(short type) {
        return dao.queryAllByType(type);
    }

    @Override
    public List<WarehouseEntity> queryList(short type, String productSN, String lineSN) throws Exception {
        return dao.queryTypeListByLineSN(type, productSN, lineSN);
    }

    @Override
    public List<WarehouseEntity> queryList(String rmSN, String lineSN) throws Exception {

        return dao.queryPortListByLineSN(rmSN, lineSN);
    }

    @Override
    public PageResult<WarehouseEntity> queryPage(PageQuery pageQuery, int rowNum, int page) throws Exception {
        pageQuery.checkSortSides(WarehouseEntity.class);
        pageQuery.getQuery().put("querySkip", (page - 1) * rowNum);
        pageQuery.getQuery().put("pageSize", rowNum);
        //pageQuery.getQuery().put("unitSNArray", getCompanySNList());
        if (pageQuery.getQuery().get("isRawMaterials") == null) {
            pageQuery.getQuery().put("isRawMaterials", 0);
        }
        PageResult<WarehouseEntity> result = new PageResult(rowNum);
        var filters = SearchFilterEngine.generateQueryList(pageQuery.getFilters(),WarehouseEntity.class);
        result.setRecords(dao.queryCountByCondition(pageQuery.getQuery(),filters));
        result.setPage(page);
        result.setData(dao.queryPage(pageQuery.getQuery(),filters));
        return result;
    }


    @Override
    public WarehouseEntity findBySn(String sn) {
        return dao.findBySN(sn);
    }

    @Override
    @Transactional
    public Integer saveWarehouse(Map<String, Object> model, String sn) throws Exception {
        Integer result = 0;
        WarehouseEntity entity;
        if (Tl.isEmpty(sn)) {
            //执行保存
            entity = new WarehouseEntity();
            Tl.getChangeLog(entity, model);
            entity.setPYFieldValue();
            entity.setSn(queryNewSN(WarehouseEntity.class));
            entity.setOrganizationCode(getDepartSN());
            entity.setIsRawMaterials((short) 0);
            result = dao.insertNew(entity);
            if (result > 0) {
                writeLog(ActionType.CREATE, entity.getSn(), "插入仓库信息信息", Tl.toJson(entity), WarehouseEntity.class);
            }

        } else {
            //执行修改
            entity = findBySn(sn);
            var backJson = entity.toJson();
            var changeLog = Tl.getChangeLog(entity, model);
            entity.setPYFieldValue();
            result = dao.update(entity);
            if (result > 0) {
                writeLog(ActionType.UPDATE, sn, changeLog, backJson, WarehouseEntity.class);
            }
        }
        if (result > 0) {
            saveGS(
                    entity.getSn(),
                    entity.getWhName(),
                    String.join(",",
                            entity.getWhName(),
                            entity.getPlName()
                    ),
                    entity,
                    HsApp.getCurrent() != null ? HsApp.getCurrent().getUserSN() : null,
                    null
            );
        }
        return result;
    }

    @Transactional
    public Integer delete(String[] sn) {
        var result = dao.changeStateBySn(sn, DataState.DELETED.code);
        if (result > 0) {
            writeLog(ActionType.DELETE, sn, WarehouseEntity.class);
            deleteGs(sn);
        }
        return result;
    }

    @Override
    public Integer changeStateBySn(String[] snArray, short state) {
        int result=dao.changeStateBySn(snArray,state);
        if (result > 0) {
            forWriteLog(snArray,state);
        }
        return result;
    }
}